<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>秒杀商品列表页-用户登陆</title>
    <link rel="stylesheet" th:href="@{/lib/bootstrap.min.css}"/>
    <link rel="stylesheet" th:href="@{/lib/font/css/font-awesome.min.css}"/>
    <link rel="stylesheet" th:href="@{/css/seckill.css}"/>
    <link rel="stylesheet" th:href="@{/css/public.css}"/>
</head>
<style>

</style>
<body>
<div class="container">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h1 align="center">这是用户登录页</h1>
            <div>
                <table align="center" border="0">
                    <tr>
                        <td>用户名:</td>
                        <td>
                            <input id="username" type="text" name="username"/>
                        </td>
                    </tr>
                    <br/>
                    <tr>
                        <td>密&nbsp;&nbsp;码:</td>
                        <td>
                            <input id="password" type="password" name="password"/>
                        </td>
                    </tr>
                    <tr>
                        <td><button id="login" class="btn btn-primary">登陆</button></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript" th:src="@{/lib/jquery-3.3.1.min.js}"></script>
<script type="text/javascript" th:src="@{/lib/bootstrap.min.js}"></script>

<script type="text/javascript">
    $("#login").click(function () {
        $.ajax({
            type: "POST",
            url: "http://localhost:8092/kill/login",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify(getJsonData()),
            success:function (res) {
                if (res.code === 200) {
                    setCookie("username", getJsonData().username, 7)
                    window.location.href = "/kill/list"
                } else {
                    window.location.href = "/kill/error"
                }
            }
        })


    })

    function getJsonData() {
        var username = $("#username").val()
        var password = $("#password").val()
        var data = {
            "username": username,
            "password": password
        }
        return data
    }

    function setCookie(cname, cvalue, exdays) {
        var expires = exdays;
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/kill";
    }
</script>

</body>
</html>